*discuss top ranked method at all
use "${PilotPublicData}/hgopy_pilot_counselling_public.dta", clear 

*local for whether we use adopted1 or adopted global 
local 1ornot = "adopted_method"

*create an indicator for adopting third ranked method
gen adopttrm=0 if `1ornot'==0
replace adopttrm=1 if `1ornot'==mr3 
replace adopttrm=2 if `1ornot'!=mr3 & `1ornot'!=0
label define adopttrm 0 "None" 1 "Third-ranked method" 2 "Other than second-ranked"
label val adopttrm adopttrm
label var adopttrm "Adopted third-ranked method"
*create an indicator for adopting fourth ranked method
gen adoptform=0 if `1ornot'==0
replace adoptform=1 if `1ornot'==mr4 
replace adoptform=2 if `1ornot'!=mr4 & `1ornot'!=0
label define adoptform 0 "None" 1 "Fourth-ranked method" 2 "Other than second-ranked"
label val adoptform adoptform
label var adoptform "Adopted fourth-ranked method"
*display adopted third or lower ranked method
gen adoptlower = (adopttrm == 1 | adoptform == 1)
label define adoptlower 0"None" 1"Adopted"
label val adoptlower adoptlower 
label var adoptlower "Adopted third- or lower-ranked method"

local N = _N
// Whether the top ranked `i' was discussed
local methods = " method1 method2 method3 method4 method5 "
forvalues t = 1/5 {
	gen trmd`t' = .

  foreach method in `methods' {
    forvalues i = 1(1)`N' {
      local val = ndm[`i']
      
      if (`val' != 0) {
        forvalues mm = 1(1)`val'{
          replace trmd`t' = 1 if d_method`mm' == mr`t' & !missing(d_discuss`mm') & _n== `i'
        } 
      } 
    }
  } 
  replace trmd`t' = 0 if missing(trmd`t')
}
// TRMD (Zero)
gen trmd0 = (ndm==0)

*Fix cases when there was a discussion and the mr variables were not full (maybe due to complications n = 1)
gen x = .
forvalues x = 1/5 {
  replace x = 1 if trmd`x' == 1 & ndm == 1
}  
replace trmd4 = 1 if ndm == 1 & x == .
drop x
*Same for ndm == 2 (the total should be 73 including mim)
gen trmd35 = (trmd3 == 1 | trmd4 == 1 | trmd5 == 1)
gen x = .
replace x = 1 if trmd1  == 1  & trmd2  == 1 & ndm == 2  // Top and second
replace x = 1 if trmd1  == 1  & trmd35 == 1 & ndm == 2  // Top and 3+
replace x = 1 if trmd2  == 1  & trmd35 == 1 & ndm == 2  // Second and 3+ 
replace x = 1 if trmd35 == 1  & trmd1!= 1 & trmd2!=1 & ndm == 2                // 3+ 

count if x == 1 & ndm == 2
local x = r(N)
sum ndm if ndm ==2
local n = r(N)
assert `x' == `n'
egen xx = rowtotal(trmd1-trmd5) if ndm ==2
assert xx == ndm if ndm ==2 
drop x xx

gen x1x2 = 1 if trmd1  == 1  & trmd2  == 1  & ndm == 2 
gen x1x3 = 1 if trmd1  == 1  & trmd35 == 1  & ndm == 2
gen x2x3 = 1 if trmd2  == 1  & trmd35 == 1  & ndm == 2
gen x3x3 = 1 if trmd35 == 1  & trmd1!= 1    & trmd2!=1 & ndm == 2   
drop x?x?

*only sdm and idm
keep if mim == 0
tab mr1 mr2, cell 

gen mr_iud_imp    = (mr1 == 1) & (mr2 == 2) & !missing(mr2)  
gen mr_imp_iud    = (mr1 == 2) & (mr2 == 1) & !missing(mr2)
gen mr_iud_sarc   = (mr1 == 1) & !inlist(mr2, 1, 2) & !missing(mr2)
gen mr_imp_sarc   = (mr1 == 2) & !inlist(mr2, 1, 2) & !missing(mr2)
gen mr_pill_other = (mr1 == 3) & (mr2 != 3)
gen mr_lam_other  = (mr1 == 5) & (mr2 != 5) 
gen mr_other      = inlist(mr1,3,5) & (mr2 != 3 | mr2 != 5) 
// gen mr_other      = (mr_iud_imp != 1 & mr_imp_iud != 1 & mr_iud_sarc != 1 & mr_imp_sarc != 1 )
gen mr_all        = 1

*Fix adoptfrm and adoptsrm to be just a dummy instead of 3 cat
foreach var of varlist adoptfrm adoptsrm {
  replace `var' = 0 if `var' == 2
}

*New LARCs
foreach var of varlist adoptfrm adoptsrm adoptlower {
  gen larc_`var' = (`var' == 1)
  replace larc_`var' = 0 if adopted_larc == 0
}

*Create matrix for all outcomes
 
local outcomes = " ndm trmd1 trmd2 trmd35 adopted_larc larc_adoptfrm larc_adoptsrm larc_adoptlower "
local cols     = " mr_iud_imp mr_imp_iud mr_iud_sarc mr_imp_sarc mr_other mr_all "
capture matrix drop _all
capture drop v1 v2 
tab view, gen(v)

foreach col in `cols' {
  foreach var in `outcomes' {
    *Number of observations
    reg `var' v1 v2 if `col' == 1, nocons
    local nn = e(N)
    *Mean values and SD values for IDM (1) and SDM (2)
    ttest `var' if `col' == 1, by(view)
    local b1  = r(mu_1)
    local sd1 = r(sd_1)
    local b2  = r(mu_2)
    local sd2 = r(sd_2)
    *Diff SDM - IDM: Coef and SEs
    reg `var' b1.view if `col'==1
    lincom 2.view 
    local b3  = r(estimate)
    local se3 = r(se)

    matrix M_`col' = nullmat(M_`col') \ (`b1' \ `sd1' \ `b2' \ `sd2' \ `b3' \ `se3')
  }
  matrix M_`col' = nullmat(M_`col') \ (`nn')
}

matrix M = (M_mr_iud_imp, M_mr_imp_iud, M_mr_iud_sarc, M_mr_imp_sarc, M_mr_other, M_mr_all)
matrix list M

*show results and make a nice table 
clear 
matrix list M
svmat M

local N = c(N)
*stars and SEs cleaning up
forvalues n=1/6 {
  gen s`n'=""
  gen XM`n'=string(M`n',"%11.3f")+s`n'
  forvalues rr = 2(2)`N'{
     replace XM`n'="("+XM`n'+")" if inlist(_n,`rr') 
  }
  replace XM`n'="" if XM`n'=="."
  replace XM`n'=subinstr(XM`n',".000","",.) if _n>6
  drop s`n' M`n'
  *removed those rows with coef == 0
  replace XM`n' = "-" if inlist(XM`n', "0", "(0)") 
}

replace XM1 = "-" if inlist(XM1, "0", "(0)") 
replace XM2 = "-" if inlist(XM2, "0", "(0)")
replace XM4 = "0.000" if inlist(XM4, "-") & _n == 43

gen M0=""
order M0, first 

local firstcol = `" "Number of methods discussed (IDM)" " " "Number of methods discussed (SDM)" " " "SDM-IDM" " " "Discussed top ranked method (IDM)" " " "Discussed top ranked method (SDM)" " " "SDM-IDM" " " "Discussed second ranked method (IDM)" " " "Discussed second ranked method (SDM)" " " "SDM-IDM" " " "Discussed the third- or lower-ranked method (IDM)" " " "Discussed the third- or lower-ranked method (SDM)" " " "SDM-IDM" " " "Adopted LARC (IDM)" " " "Adopted LARC (SDM)" " " "SDM-IDM" " " "Adopted LARC FRM (IDM)" " " "Adopted LARC FRM (SDM)" " " "SDM-IDM" " " "Adopted LARC SRM (IDM)" " " "Adopted LARC SRM (SDM)" " " "SDM-IDM" " " "Adopted LARC TLRM (IDM)" " " "Adopted LARC TLRM (SDM)" " " "SDM-IDM" " " "Obs. "' 

local i=0
foreach j of local firstcol {
	local ++i 
	replace M0="`j'" if _n==`i'
}

* Remove sds but not ses
drop if inlist(_n, 2,4,8,10,14,16,20,22,26,28,32,34,38,40,44,46)
gen n = _n

* Add obs.
set obs `=_N+8'
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel A. Number of methods discussed}}" ///
  if _n == 34
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel B. Discussed top ranked method}}" ///
  if _n == 35
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel C. Discussed second ranked method}}"  ///
  if _n == 36
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel D. Discussed third- or lower-ranked method}}" ///
  if _n == 37
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel E. Adopted a LARC}}"  ///
  if _n == 38
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel F. Adopted a LARC that was top-ranked method}}"  ///
  if _n == 39
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel G. Adopted a LARC that was second-ranked method}}" /// 
  if _n == 40
replace M0 = "\multicolumn{7}{@{}l}{\textbf{Panel H. Adopted a LARC that was third- or lower-ranked method}}" ///
  if _n == 41
 
local i = 0.5
forvalues j = 34/41 {
  replace n = `i' if _n == `j'
  local i = `i' + 4
}  

sort n 
drop n 

local j = 2
forvalues i = 2(5)41 {
  local j = `i' + 1
  replace M0 = "IDM" if _n == `i'
  replace M0 = "SDM" if _n == `j'
}
*add & signs
ds M0, not
foreach v in `r(varlist)' {
	replace `v' = "& " + `v' if `v'!=""
} 

*combine into one row
gen X=M0+XM1+XM2+XM3+XM4+XM5+XM6
keep X
*export 
run "${Programs}/texsave_custom.do"
#delimit ;
texsave_custom using "${OutTexTab}/Tab02-het-teffects-replication.tex", 
replace nonames nofix hlines(5 10 15 20 25 30 35 40 )  frag
	align("l*{6}{c}")
	headerlines(" & (1) & (2) & (3) & (4) & (5) & (6) " 
        " & \multicolumn{5}{c}{1st-Ranked-Method - 2nd-Ranked-Method} & All"
				" \cmidrule{2-6} & IUD-Implant	& Implant-IUD	&	IUD-SARC & IMP-SARC &	Other ") ;
#delimit cr

